/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gestion;

import operacionesDB.AbstractDB;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Luis Felipe Gomez
 */
public class DBgestionLibros extends AbstractDB {
    
    private ResultSet rs;
    private PreparedStatement r;
    
    public DBgestionLibros() {
        rs = null;
        r = null;
    }
    
    public ResultSet todosLibros() {
        try {
            
            r = (PreparedStatement) this.con.prepareStatement("call MostrarTodosLibros ");
            rs = r.executeQuery();

            /*
             * while (rs.next()) { System.out.println(""+ rs.getString(1)+
             * rs.getString(2) + rs.getString(3) + rs.getString(4)); }
             */
        } catch (SQLException ex) {
            Logger.getLogger(DBgestionLibros.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }
    
    public void eliminarLibros(int t) {
        try {
            int j = 0;
            r = (PreparedStatement) this.con.prepareStatement("call eliminarLibro (?)");
            r.setInt(1, t);
            r.execute();
            j = r.getUpdateCount();
            if (j == 1) {
                
                JOptionPane.showMessageDialog(null, "la eliminación a sido a exitosa");
            } else {
                
                JOptionPane.showMessageDialog(null, "no se a realizado ninguna eliminación");
            }
            
            
            
        } catch (SQLException ex) {
            Logger.getLogger(DBgestionLibros.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public ResultSet BuscarIsbn(int t) {
        try {
            
            r = (PreparedStatement) this.con.prepareStatement("call buscarIsbn (?) ");
            r.setInt(1, t);
            rs = r.executeQuery();
            
        } catch (SQLException ex) {
            Logger.getLogger(DBgestionLibros.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }
    
    public void libroNuevo(int isbnNuevo, String titulo, int reservado, int prestado) {
        
        
        try {
            
            r = (PreparedStatement) this.con.prepareStatement("call nuevoLibro(?, ? , ? , ? ) ");
            r.setString(1, titulo);
            r.setInt(2, reservado);
            r.setInt(3, prestado);
            r.setInt(4, isbnNuevo);
            rs = r.executeQuery();
            
            
        } catch (SQLException ex) {
            Logger.getLogger(DBgestionLibros.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public void ModificarTitulo(int i, String o) {
        try {
            r = (PreparedStatement) this.con.prepareStatement("call modificarTitulo( ? , ?)");
            r.setInt(1, i);
            r.setString(2, o);
            r.executeUpdate();
            int f = r.getUpdateCount();
            if (f == 1) {
                JOptionPane.showMessageDialog(null, " se realizó la opración ");
                
            } else {
                JOptionPane.showMessageDialog(null, " no realizó la opración ");
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(DBgestionLibros.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public int maxIsbn() {
        int f = 0;
        try {
            r = (PreparedStatement) this.con.prepareStatement("call MaximoIsbn");
            rs = r.executeQuery();
            
            while (rs.next() == true) {
                f = rs.getInt(1);
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(DBgestionLibros.class.getName()).log(Level.SEVERE, null, ex);
        }
        return f;
    }
    
    public int countLib() {
        rs = null;
        int d = 0;
        try {
            r = (PreparedStatement) con.prepareStatement("call countLib");
            rs = r.executeQuery();
            while (rs.next() == true) {
                d = rs.getInt(1);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBGestionUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }
        return d;
    }
    
    public void editarReserva(int i) {
        rs = null;
        try {
            r = (PreparedStatement) con.prepareStatement("call editarResLib(?)");
            r.setInt(1, i);
            rs = r.executeQuery();
        } catch (SQLException ex) {
            Logger.getLogger(DBgestionLibros.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
}
